package com.xxx.video.main.fragments;

import android.os.Bundle;

import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.xxx.video.R;
import com.xxx.video.commen.fragments.BaseFragment;
import com.xxx.video.commen.utils.ViewPager2Utils;
import com.xxx.video.main.adapters.HomePagerAdapter;
import com.xxx.video.main.beans.HomeTypeListBean;
import com.xxx.video.main.presenters.HomeFragmentPresenter;
import com.xxx.video.main.presenters.IHomeView;

import java.util.List;
import java.util.Objects;

import butterknife.BindView;

public class HomeFragment extends BaseFragment<HomeFragmentPresenter> implements IHomeView {


    @BindView(R.id.tl_home_fragment)
    TabLayout tlHomeFragment;
    @BindView(R.id.vp_home_fragment)
    ViewPager2 vpHomeFragment;


    @Override
    protected HomeFragmentPresenter createPresenter() {
        return new HomeFragmentPresenter(Objects.requireNonNull(getActivity()), this);
    }

    public static HomeFragment newInstance(Bundle args) {
        HomeFragment fragment = new HomeFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void refreshData(Bundle extras) {

    }

    @Override
    public int getContentLayout() {
        return R.layout.fragment_home;
    }

    @Override
    public void initData(Bundle extras) {
        ViewPager2Utils.getInstance().reduceDragSensitivity(vpHomeFragment);
        getPresenter().getTypeList();
    }


    @Override
    public void showRootLayoutStatus(int status) {
        showLayoutStatus(status);
    }


    @Override
    public void initTabLayout(List<HomeTypeListBean.TypeListBean> typeList) {
        showLayoutStatus(1);
        HomePagerAdapter homePagerAdapter = new HomePagerAdapter(Objects.requireNonNull(getActivity()));
        vpHomeFragment.setAdapter(homePagerAdapter);
        homePagerAdapter.setData(typeList);
        vpHomeFragment.setOffscreenPageLimit(homePagerAdapter.getItemCount());
        new TabLayoutMediator(tlHomeFragment, vpHomeFragment, (tab, position) -> {
            tab.setText(typeList.get(position).fName);

        }).attach();
    }


}
